package com.novoda.runbuddy.controller;

import android.location.Location;
import android.util.Log;

import com.novoda.runbuddy.model.CheckPoint;
import com.novoda.runbuddy.model.CommonGeoLocation;
import com.novoda.runbuddy.model.Run;
import com.novoda.runbuddy.model.RunSegment;
import com.novoda.runbuddy.util.DBHelperImpl;

public class RunControllerImpl implements IRunController {

	public Run			activeRun					= null;
	protected boolean	aRunIsCurrentlyActive = false;
	private String		TAG							= "[RBuddy]:" + "RunControllerImpl";
	private int			registeredNoOfCheckPoints	= 0;
	private int	checkpointNo = 0;

	public RunControllerImpl() {
	}

	public void startNewRun(DBHelperImpl dbHelper, String name, long startTime) {
		this.activeRun = new Run(dbHelper, name, startTime);
		aRunIsCurrentlyActive = true;
	}

	public void startNewRun(long id, String name, long startTime) {
		this.activeRun = new Run(id, name, startTime);
		aRunIsCurrentlyActive = true;
	}

	public boolean aRunIsCurrentlyActive() {
		return aRunIsCurrentlyActive;
	}
	
	
	// TODO kevin: add a new run on the route.
	public void addCheckPoint(Location location) {

		this.activeRun.addRoutePoint(location); //A route is being added in correlation with each run just now
		int id = 0, serverId = 0;
		
		CheckPoint checkPoint = new CheckPoint(id, new CommonGeoLocation(location), serverId, "checkPoint", 0, "name");
		RunSegment runSeg = new RunSegment(0, checkPoint, checkpointNo, 0, location.getTime(), System.currentTimeMillis());
		this.checkpointNo++;
		
		
		
		if (aRunIsCurrentlyActive =false){
			this.activeRun.addLastSegment(runSeg);
//TODO: kevin This mess of comment code is here because I can't run the unit tests with the logs but I am testing using the instrumentations too.
//			Log.d(TAG, "Adding LAST RunSegment: " 
//					+ "Run Name=[" + activeRun.name + "], "
//					+ "Route Name=[" + activeRun.routeName + "], "
//					+ "Start Time=[" + activeRun.startTime + "], " + "Stop Time=[" + activeRun.stopTime + "], "
//					+ "Run ID=["+ activeRun.id + "], " + "Route ID=[" + activeRun.routeId + "], "
//					+ "RunSegments Size=["+activeRun.getRunSegments() + "]");
		}else{
			
			if(registeredNoOfCheckPoints < 2){	
				this.activeRun.addFirstRunSegment(runSeg);
//				Log.d(TAG, "Adding FIRST RunSegment: " 
//						+ "Run Name=[" + activeRun.name + "], "
//						+ "Route Name=[" + activeRun.routeName + "], "
//						+ "Start Time=[" + activeRun.startTime + "], " + "Stop Time=[" + activeRun.stopTime + "], "
//						+ "Run ID=["+ activeRun.id + "], " + "Route ID=[" + activeRun.routeId + "], "
//						+ "RunSegments Size=["+activeRun.getRunSegments() + "]");			
			
			
			
			}else{
				this.activeRun.addRunSegment(runSeg);
//				Log.d(TAG, "Adding RunSegment: " 
//						+ "Run Name=[" + activeRun.name + "], "
//						+ "Route Name=[" + activeRun.routeName + "], "
//						+ "Start Time=[" + activeRun.startTime + "], " + "Stop Time=[" + activeRun.stopTime + "], "
//						+ "Run ID=["+ activeRun.id + "], " + "Route ID=[" + activeRun.routeId + "], "
//						+ "RunSegments Size=["+activeRun.getRunSegments() + "]");
				
			}
			
		}

	}


	public long getCurrentRunID() {
		return this.activeRun._id;
	}

	public void stopRun(long stopTime) {
		activeRun.stopTime = stopTime;
		aRunIsCurrentlyActive = true;
//		addNewRunOnRoute();
	}

	public void stopRun(Location loc, long stopTime) {
		activeRun.stopTime = stopTime;		
		aRunIsCurrentlyActive = true;
	}

	public Run getActiveRun() {
		return activeRun;
	}


}
